﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Gencode.Web.Common
{
    public class gPage : System.Web.UI.Page
    {
        protected const string layoutPageDefault = "/modules/Layout.ascx";
        //protected const string LayoutHeaderName = "/modules/Header.ascx";
        //protected const string LayoutFooterName = "/modules/Footer.ascx";
        //protected const string layoutMenuName = "/modules/mainmenu.ascx";

		protected string layoutPage = "";
		protected bool showHeaderFooterControls = true;
		protected Control layoutFooterControl;
		protected Control layoutHeaderControl;
		protected Control layoutMenuControl;
        protected Control layoutLeftControl;
        protected Control layoutRightControl;
		protected ITemplatePage gPageTemplate;

		override protected void OnInit(EventArgs e)
		{
			base.OnInit(e);
            //if (!gUtil.InDesign)
            //{
            //    if (layoutHeaderControl == null && showHeaderFooterControls)
            //        LoadHeaderControl();
            //    if (layoutFooterControl == null && showHeaderFooterControls)
            //        LoadFooterControl();
            //}
		}

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

		protected void LoadTemplate()
		{
			gPageTemplate = (ITemplatePage)LoadControl(LayoutTemplatePage);
			base.Controls.Add((Control)gPageTemplate);
		}

		protected override void AddParsedSubObject(object obj)
		{
			if (gPageTemplate == null)
				LoadTemplate();

			gPageTemplate.AddControlToBody((Control)obj);
		}

		protected override void CreateChildControls()
		{
            ////header-footer	
            //if (gPageTemplate == null)
            //    LoadTemplate();

            //if (layoutHeaderControl == null)
            //    LoadHeaderControl();
            //gPageTemplate.AddHeader(layoutHeaderControl);

            //if (layoutFooterControl == null)
            //    LoadFooterControl();
            //gPageTemplate.AddFooter(layoutFooterControl);

            //if (layoutMenuControl == null)
            //    LoadMenuControl();
            //gPageTemplate.AddTopControl(layoutMenuControl);

			base.CreateChildControls();
		}

		protected void LoadHeaderControl(string controlName)
		{
            //layoutHeaderControl = LoadControl(LayoutHeaderName);
            layoutHeaderControl = LoadControl(controlName);
		}

        protected void LoadFooterControl(string controlName)
		{
            //layoutFooterControl = LoadControl(LayoutFooterName);
            layoutFooterControl = LoadControl(controlName);
		}

        protected void LoadMenuControl(string controlName)
		{
            //layoutMenuControl = LoadControl(layoutMenuName);
            layoutMenuControl = LoadControl(controlName);
		}

        protected void LoadLeftControl(string controlName)
        {
            layoutLeftControl = LoadControl(controlName);
        }

        protected void LoadRightControl(string controlName)
        {
            layoutRightControl = LoadControl(controlName);
        }

		#region Properties
		public virtual string LayoutTemplatePage
		{
			get
			{
				if (layoutPage == String.Empty)
				{
					layoutPage = layoutPageDefault;
				}

				return layoutPage;
			}
			set
			{
				layoutPage = value;
			}
		}
		public bool ShowHeaderFooterControls
		{
			get
			{
				return showHeaderFooterControls;
			}
			set
			{
				showHeaderFooterControls = value;
			}
		}
		#endregion
	}
}
